﻿using System;
using System.Globalization;

namespace Base.Common.Converters
{
    public class EnumDescriptionConverter : ConverterBase
    {
        public Type EnumType { get; set; }

        public override object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            if (value != null)
            {
                try
                {
                    var @enum = value as Enum;
                    if (@enum != null)
                    {
                        return @enum.GetDescription();
                    }
                    if (EnumType != null)
                    {
                        var em = Enum.Parse(EnumType, value.ToString());
                        return ((Enum)em).GetDescription();
                    }
                }
                catch (Exception)
                {
                    //
                }
            }
            return string.Empty;
        }

        public override object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }
}
